package com.decoration.basic.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecArticleInf;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.findCondition.DecArticleFindCondition;
import com.decoration.basic.service.DecArticleInfService;

@Controller
@RequestMapping("/admin")
public class ArticleController extends BasicController {

	@Resource
	private DecArticleInfService decArticleInfService;

	@RequestMapping(value = "/article/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addCarousel(JsonResult jsonResult, @RequestBody DecArticleInf decArticleInf) {
		if (null == decArticleInf || "".equals(decArticleInf)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要添加的文章信息为空,请重试!");
			return jsonResult;
		}

		int res = decArticleInfService.insert(decArticleInf);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("文章添加失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("添加成功");
		return jsonResult;
	}

	@RequestMapping(value = "/article/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateCarousel(JsonResult jsonResult, @RequestBody DecArticleInf decArticleInf) {
		if (null == decArticleInf || "".equals(decArticleInf)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的文章信息为空,请重试!");
			return jsonResult;
		}
		if (null == decArticleInf.getArticleId()) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的文章ID为空,请重试!");
			return jsonResult;
		}
		int res = decArticleInfService.updateByPrimaryKeySelective(decArticleInf);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("文章更新失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("更新成功");
		return jsonResult;
	}

	@RequestMapping(value = "/article/del", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delCarousel(JsonResult jsonResult, @RequestBody DecArticleInf decArticleInf) {
		if (null == decArticleInf.getArticleId()) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要删除的文章ID为空,请重试!");
			return jsonResult;
		}
		int res = decArticleInfService.deleteByPrimaryKey(decArticleInf.getArticleId());
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("文章删除失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("删除成功");
		return jsonResult;
	}

	@RequestMapping(value = "/article/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getCarouselList(JsonResult jsonResult, @RequestBody DecArticleFindCondition findCondition) {
		setPagerInfo(findCondition);
		Integer count = decArticleInfService.getArticleListSize();
		List<DecArticleInf> list = decArticleInfService.getArticleList(findCondition);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(count);
		jsonResult.setData(list);
		return jsonResult;
	}

	@RequestMapping(value = "/article/{articleId}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarousel(JsonResult jsonResult, @PathVariable Integer articleId) {
		DecArticleInf decArticleInf = decArticleInfService.selectByPrimaryKey(articleId);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setData(decArticleInf);
		return jsonResult;
	}
}
